package com.youlai.boot.modules.contact.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.contact.converter.ContactConverter;
import com.youlai.boot.modules.contact.mapper.ContactMapper;
import com.youlai.boot.modules.contact.model.Contact;
import com.youlai.boot.modules.contact.model.ContactForm;
import com.youlai.boot.modules.contact.model.ContactVO;
import com.youlai.boot.modules.contact.service.ContactService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements ContactService {

    private final ContactConverter contactConverter;

    @Override
    public Page<ContactVO> getContactPage(Long merchantId, int pageNum, int pageSize) {
        Page<ContactVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.getContactPage(page, merchantId);
    }

    @Override
    public ContactVO getContactById(Long id) {
        Contact contact = this.getById(id);
        return contactConverter.toVO(contact);
    }

    @Override
    public Long saveContact(ContactForm form) {
        Contact contact = contactConverter.toEntity(form);
        this.save(contact);
        return contact.getId();
    }

    @Override
    public void updateContact(ContactForm form) {
        Contact contact = contactConverter.toEntity(form);
        this.updateById(contact);
    }

    @Override
    public boolean deleteContact(Long id) {
        return this.removeById(id);
    }
}